<?php
/*
 * *********************************************************
 * ## Module controllers must extend MD_Controller class ##
 * *********************************************************
 */
class Ajax extends MD_Controller {
    
public function __construct()
{
    parent::__construct();

	/*
	 * ******************************************************************
	 * CHECK IF MODULE IS INSTALLED PROPERLY
	 * ******************************************************************
	 */
	$this->MODULE->config->load('module_config');	
	$short_name = $this->MODULE->config->item('short_name');
	$settings_table = $this->MODULE->config->item('settings_table');	 
	if(!$this->PACKAGE->security->app_modules->module_exist($short_name) || empty($short_name) || empty($settings_table))
	{
		$this->PACKAGE->errors->generate->code("001")->stop();
	}
    
            /*
             * ********************************************
             * LOAD LANGUAGE FILES FOR THIS MODULE ONLY
             * notice that some language files have been
             * loaded within MY_Controller core class
             * and those language files are common for all
             * modules
             * ********************************************
             */    
    
            $this->MODULE->lang->load('module_texts',LANGUAGE);
            
            /*
             * ********************************************
             * LOAD PACKAGES
             * ********************************************
             */
            $this->PACKAGE->core->packages->add('regional');   
			$this->PACKAGE->core->packages->add('routing');   
      
			/*
			 * ********************************************
			 * LOAD LIBRARIES
			 * ********************************************
			 */
			$this->MODULE->load->library('module_trunks');
	  
            /*
             * ********************************************
             * LOAD MODELS
             * ********************************************
             */
            $this->PACKAGE->regional->load->model('countries');
            $this->PACKAGE->regional->load->model('timezone');
			
			$this->APPLICATION->load->model('app_mod_trunks');
			$this->MODULE->load->model('module_mod_trunks');
						
}
public function simulate_routing($origin_trunk_id,$codec_field)
{
	$post = $this->input->post('data');

	$linked_family_id = $this->PACKAGE->routing->trunks->get_linked_family_id($origin_trunk_id);
	
	$family = $this->PACKAGE->routing->trunks->get_family_as_array_by_id($linked_family_id);

	$termination_trunk_ids = $this->PACKAGE->routing->trunks->route_trunk($origin_trunk_id,$codec_field);
	
	$text = "<b>".lang('text_routing_family').": ".$family['short_name']."</b><br />";
	$text .= "<b>".lang('term_exceptions').": ".$this->PACKAGE->routing->trunks->calculate_exceptions($origin_trunk_id)."</b>";
	
	$text .= "<fieldset class='ui-widget-content ui-corner-all response-field'>";
	$text .= "<legend class='ui-widget-content ui-corner-all'>".lang('text_active_routing')."</legend>";
	$text .= "<ul class='response-list'>";
	foreach($termination_trunk_ids as $trunk_id)
	{
		$trunk = $this->PACKAGE->routing->trunks->get_trunk_as_array_by_id($trunk_id,'termination');
		$carrier = $this->PACKAGE->routing->carriers->get_carrier_as_array_by_id($trunk['carrier_id']);
		
		if(@$post['active_routing_only'] != "1")
		{
		// detect extra trunks
		$query = $this->db->where('origin_trunk_id',$origin_trunk_id)->where('termination_trunk_id',$trunk_id)->where('family_id',NULL)->where('status','1')->get('relations_origin_termination');
		if($query->num_rows() > 0){$extra_trunks[] = $query->row_array();}
		
		// detect duplicates
		$query = $this->db->where('origin_trunk_id',$origin_trunk_id)->where('termination_trunk_id',$trunk_id)->where('status','1')->get('relations_origin_termination');
		if($query->num_rows() > 1){$duplicates[] = $query->row_array();}			
		}
		
		$text .= "<li>".$carrier['username']." - ".$trunk['short_name']."</li>";
	}
	$text .= "</ul></fieldset>";

	if(@$post['active_routing_only'] != "1")
	{	
	// detect disallowed
	$query = $this->db->where('origin_trunk_id',$origin_trunk_id)->where('allowed','0')->order_by('termination_carrier_username','ASC')->order_by('termination_trunk_id','ASC')->get('relations_origin_termination');
	if($query->num_rows() > 0)
	{
	$text .= "<fieldset class='ui-widget-content ui-corner-all response-field'>";
	$text .= "<legend class='ui-widget-content ui-corner-all'>".lang('text_disallowed')."</legend>";
	$text .= "<ul class='response-list'>";
	foreach($query->result_array() as $entry)
	{
		$trunk = $this->PACKAGE->routing->trunks->get_trunk_as_array_by_id($entry['termination_trunk_id'],'termination');
	
		$text .= "<li>".$entry['termination_carrier_username']." - ".$trunk['short_name']."</li>";
	}
	$text .= "</ul></fieldset>";
	}

	
	if(is_array(@$extra_trunks) && count(@$extra_trunks) > 0)
	{
	$text .= "<fieldset class='ui-widget-content ui-corner-all response-field'>";
	$text .= "<legend class='ui-widget-content ui-corner-all'>".lang('text_extra_trunks')."</legend>";
	$text .= "<ul class='response-list'>";
	foreach($extra_trunks as $entry)
	{
		$trunk = $this->PACKAGE->routing->trunks->get_trunk_as_array_by_id($entry['termination_trunk_id'],'termination');
	
		$text .= "<li>".$entry['termination_carrier_username']." - ".$trunk['short_name']."</li>";
	}
	$text .= "</ul></fieldset>";
	}
	
	
	if(is_array(@$duplicates) && count(@$duplicates) > 0)
	{	
	$text .= "<fieldset class='ui-widget-content ui-corner-all response-field'>";
	$text .= "<legend class='ui-widget-content ui-corner-all'>".lang('text_duplicates')."</legend>";
	$text .= "<ul class='response-list'>";
	foreach($duplicates as $entry)
	{
		$trunk = $this->PACKAGE->routing->trunks->get_trunk_as_array_by_id($entry['termination_trunk_id'],'termination');
	
		$text .= "<li>".$entry['termination_carrier_username']." - ".$trunk['short_name']."</li>";
	}
	$text .= "</ul></fieldset>";	
	}
	}
	
	print $text;
}

public function load_simulator_origin_trunks($carrier_id)
{
	$trunks = $this->APPLICATION->app_mod_trunks->get_trunks_by_carrier($carrier_id,'origin');

	$text = "<option value='none'>--</option>";
	foreach($trunks as $trunk)
	{
		$text .= "<option value='".$trunk['id']."'>".$trunk['short_name']."</option>";
	}
	print $text;
}

public function load_simulator_codecs($trunk_id)
{
	$codecs = $this->PACKAGE->routing->trunks->get_trunk_codecs($trunk_id,'origin');

	$text = "<option value='none'>--</option>";
	foreach($codecs as $codec_field)
	{
		$codec = $this->PACKAGE->routing->codecs->get_codec_by_field_name($codec_field);
	
		$text .= "<option value='".$codec['codec_field']."'>".$codec['codec_name']."</option>";
	}
	print $text;
}


public function is_family_deletion_allowed($family_id)
{
	$response = $response = $this->MODULE->module_mod_trunks->is_family_deletion_allowed($family_id);	
	
    print $response;   	
}

public function route_origin_trunk($trunk_type)
{
	$post = $this->input->post('data');

	$response = $this->MODULE->module_mod_trunks->route_origin_trunk($post);	

    print $response;   
}


public function load_family_trunks($family_id)
{
	$this->PACKAGE->routing->load->library('trunks');

	$trunks = $this->PACKAGE->routing->trunks->get_family_trunks($family_id);
	
	$family = $this->PACKAGE->routing->trunks->get_family_as_array_by_id($family_id);
	
	//$text = $family['short_name'] . "|";
	$text = "";
	
	if(count($trunks) > 0)
	{
		foreach($trunks as $trunk)
		{
			$car = $this->PACKAGE->routing->carriers->get_carrier_as_array_by_id($trunk['carrier_id']);
			$text .= "<div class='row'><span class='virtual-links' rel='".$trunk['id']."'>".$car['username']." -".$trunk['short_name']."</span>&nbsp;&nbsp;&nbsp;";
			$text .= "<span class='radio'><input type='radio' name='data[family_trunks][".$trunk['id']."]' value='0' /><label>".lang('text_disallowed')."</label></span>";			
			$text .= "<span class='radio'><input type='radio' name='data[family_trunks][".$trunk['id']."]' value='1' checked='checked' /><label>".lang('text_allowed')."</label></span>";			
			$text .= "<a class='open-event' href='javascript:void(0);' title='".$this->MODULE->module_trunks->show_status_desc($trunk['id'],"termination")."'>".$this->MODULE->module_trunks->show_status_msg($trunk['id'],"termination")."</a>"."&nbsp;&nbsp;&nbsp;";
			$text .= "</div>";
		}
	}
	else
	{
		$text .= lang('term_no_trunks_linked');
	}
	print $text;
}

public function view_family_trunks($family_id)
{
	$this->PACKAGE->routing->load->library('trunks');

	$trunks = $this->PACKAGE->routing->trunks->get_family_trunks($family_id);
	
	$family = $this->PACKAGE->routing->trunks->get_family_as_array_by_id($family_id);
	
	$text = $family['short_name'] . "|";
	
	if(count($trunks) > 0)
	{
		foreach($trunks as $trunk)
		{
			$car = $this->PACKAGE->routing->carriers->get_carrier_as_array_by_id($trunk['carrier_id']);
			$text .= "<a href='".MODULE_SITE_URL."/termination/#".$trunk['id']."'>" . $car['username'] . " - ".$trunk['short_name']."</a>";
			$text .= "<a class='open-event' href='javascript:void(0);' title='".$this->MODULE->module_trunks->show_status_desc($trunk['id'],"termination")."'>".$this->MODULE->module_trunks->show_status_msg($trunk['id'],"termination")."</a>"."&nbsp;&nbsp;&nbsp;"."<br />";
		}
	}
	else
	{
		$text .= lang('term_no_trunks_linked');
	}
	print $text;
}

public function link_trunks()
{
	$trunks = $this->input->post('trunks');
	
	$text = "";
	if(is_array($trunks))
	{
		foreach($trunks as $id => $short_name)
		{
			$tru = $this->PACKAGE->routing->trunks->get_trunk_as_array_by_id($id,'termination');
			$car = $this->PACKAGE->routing->carriers->get_carrier_as_array_by_id($tru['carrier_id']);
			$text .= "<div class='row'><input type='hidden' name='data[trunks][]' value='".$id."' /><span class='virtual-links' rel='".$id."'>".$car['username']." - ".$short_name."</span>&nbsp;&nbsp;&nbsp;";
			$text .= "<a href='javascript:void(0);' onclick='remove_trunk(this, \"".$id."\")'>".lang('term_delete')."</a>";
			$text .= "<a class='open-event' href='javascript:void(0);' title='".$this->MODULE->module_trunks->show_status_desc($id,"termination")."'>".$this->MODULE->module_trunks->show_status_msg($id,"termination")."</a>"."&nbsp;&nbsp;&nbsp;";
			$text .= "</div>";
		}
	}
	print $text;
}

public function add_new_family()
{
	$post = $this->input->post('data');
	$response = $this->MODULE->module_mod_trunks->add_new_family($post);	
	print $response;
}

public function edit_family()
{
	$post = $this->input->post('data');
	$response = $this->MODULE->module_mod_trunks->edit_family($post);	
	print $response;
}

public function delete_family($family_id)
{	
	$response = $this->MODULE->module_mod_trunks->delete_family($family_id);	
	print $response;
}

public function enable_trunk($trunk_id,$trunk_type)
{
	/*
	 * ********************************************************************************************************************
	 * ENABLING / DISABLING TRUNK REQUIRES CHANGES IN MANY OTHER SEGMENTS OF THE APPLICATION THAT BELONG TO OTHER MODULES
	 * CONSIDERING WE DON'T WANT TO LOAD ALL MODULES THAT MUST ACT IN THIS CASE, WE DO THOSE CHANGES IN CORE (APPLICATION)
	 * LIBRARIES AND MODULES.
	 * CONSIDERING THAT THE BACKEND APPLICATION MANAGES THE BACKEND ONLY, THE LOGIC STAYS CORRECT!!!
	 * ********************************************************************************************************************
	 */ 
	$this->APPLICATION->load->library('app_trunks');

	$response = $this->APPLICATION->app_trunks->enable_trunk($trunk_id,$trunk_type);
	
	print $response;
}

public function disable_trunk($trunk_id,$trunk_type)
{
	/*
	 * ********************************************************************************************************************
	 * ENABLING / DISABLING TRUNK REQUIRES CHANGES IN MANY OTHER SEGMENTS OF THE APPLICATION THAT BELONG TO OTHER MODULES
	 * CONSIDERING WE DON'T WANT TO LOAD ALL MODULES THAT MUST ACT IN THIS CASE, WE DO THOSE CHANGES IN CORE (APPLICATION)
	 * LIBRARIES AND MODULES.
	 * CONSIDERING THAT THE BACKEND APPLICATION MANAGES THE BACKEND ONLY, THE LOGIC STAYS CORRECT!!!
	 * ********************************************************************************************************************
	 */ 
	$this->APPLICATION->load->library('app_trunks');

	$response = $this->APPLICATION->app_trunks->disable_trunk($trunk_id,$trunk_type);
	
	print $response;
}

public function disable_ip_address($ip_id,$trunk_type)
{
	$response = $this->MODULE->module_mod_trunks->disable_ip_address($ip_id,$trunk_type);
	
	print $response;
}


public function check_short_name($trunk_type)
{
	if($trunk_type == "origin")
	{
		$mysql_table = "origin_trunks";
	}
	if($trunk_type == "termination")
	{
		$mysql_table = "termination_trunks";
	}

	$post= $this->input->post('data');
	$short_name = $post['short_name'];
	$carrier_id = $post['carrier_id'];

		$response = $this->MODULE->module_mod_trunks->check_short_name($short_name,$carrier_id,$mysql_table);

		if($response['result'] == true)
		{
			$response = "true|".$response['text'];
		}
		else
		{
			$response = "false|".$response['text'];
		}

   print $response;
}

public function check_family_short_name()
{
	$post= $this->input->post('data');
	$short_name = $post['short_name'];

		$response = $this->MODULE->module_mod_trunks->check_family_short_name($short_name);

		if($response['result'] == true)
		{
			$response = "true|".$response['text'];
		}
		else
		{
			$response = "false|".$response['text'];
		}

   print $response;
}


/*    
public function save_settings()
{
	$this->MODULE->load->model('module_mod_settings');
    
    $response = $this->MODULE->module_mod_settings->save_settings($this->input->post('data'));
    
    print $response;

}
*/

public function add_new_trunk($trunk_type)
{
	$post = $this->input->post('data');
	$codecs = $this->input->post('codecs');
	
	if(is_array($codecs))
	{
		$post = array_merge($post,$codecs);
	}
	
	if($trunk_type == "origin")
	{
		$response = $this->MODULE->module_mod_trunks->add_new_trunk($post,"origin");	
	}
	elseif($trunk_type == "termination")
	{
		$response = $this->MODULE->module_mod_trunks->add_new_trunk($post,"termination");	
	}

  
    print $response;
    
}


public function edit_trunk($trunk_id,$trunk_type)
{
	$response = $this->MODULE->module_mod_trunks->edit_trunk($this->input->post('data'),$trunk_id,$trunk_type);	
  
    print $response;
    
}

}


?>
